﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using DAL;

public partial class StrategicPlan_StrategicPlanList : System.Web.UI.Page
{
    Authentication dllAuthentication = new Authentication();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    public string DepartmentSlogans = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        if (!IsPostBack)
        {
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpDepartment);

            bindGridView();
        }
        if (objUserInfo.UserName != "admin")
        {
            this.btnDel.Visible = false;
            this.btnModify.Visible = false;
            this.btnDisplay.Visible = false;
            this.btnImport.Visible = false;
        }
        BindSlogans();
    }

    void BindSlogans()
    {
        if (this.drpDepartment.SelectedValue != "请选择部门")
        {
            string SQL_queryIntroduction = "SELECT imgUrl FROM [DS_IntroductionImg] WHERE DepartmentId=" + this.drpDepartment.SelectedValue + " AND IsDefault=1 ORDER BY OrderValue";
            string SQL_queryManagementSlogans = "SELECT imgUrl FROM [DS_ManagementSlogans] WHERE DepartmentId=" + this.drpDepartment.SelectedValue + " AND IsDefault=1 ORDER BY OrderValue";
            string SQL_queryCoreevents = "SELECT imgUrl FROM [DS_CoreeventsImg] WHERE DepartmentId=" + this.drpDepartment.SelectedValue + " AND IsDefault=1 ORDER BY OrderValue";
            string SQL_queryManagementObject = "SELECT imgUrl FROM [DS_ManagementObject] WHERE DepartmentId=" + this.drpDepartment.SelectedValue + " AND IsDefault=1 ORDER BY OrderValue";
            object Introduction = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, SQL_queryIntroduction);
            object Coreevents = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, SQL_queryCoreevents);
            object ManagementObject = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, SQL_queryManagementObject);
            object ManagementSlogans = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, SQL_queryManagementSlogans);
            if (this.drpDepartment.SelectedItem.Text.Contains("本部"))
            {
                DepartmentSlogans = "<table border=1 cellpadding='0' cellspacing='1' width=100%><tr><td align=center style=\"width:25%\"><strong>前言</strong></td><td  align=center style=\"width:25%\"><strong>三件核心大事</strong></td><td  align=center style=\"width:25%\"><strong>六项管理目标</strong></td><td align=center style=\"width:25%\"><strong>战略计划完整版</strong></td></tr><tr>";
            }
            else
            {
                DepartmentSlogans = "<table border=1 cellpadding='0' cellspacing='1' width=100%><tr><td align=center style=\"width:25%\"><strong>前言</strong></td><td  align=center style=\"width:25%\"><strong>两件核心大事</strong></td><td  align=center style=\"width:25%\"><strong>四项管理目标</strong></td><td align=center style=\"width:25%\"><strong>战略计划完整版</strong></td></tr><tr>";
            }
            if (Introduction != null)
            {
                if (this.drpDepartment.SelectedItem.Text.Contains("本部"))
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",1)' target=_self><img style='width:100%;height:200px' src=" + Introduction + "></a></td>";
                }
                else
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",1)' target=_self><img style='width:100%;height:200px' src=" + Introduction + "></td>";
                }
            }
            if (Coreevents != null)
            {
                if (this.drpDepartment.SelectedItem.Text.Contains("本部"))
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",2)' target=_self><img style='width:100%;height:200px' src=" + Coreevents + "></td>";
                }
                else
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",2)' target=_self><img style='width:100%;height:200px' src=" + Coreevents + "></td>";
                }
            }
            if (ManagementObject != null)
            {
                if (this.drpDepartment.SelectedItem.Text.Contains("本部"))
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",3)' target=_self><img style='width:100%;height:200px' src=" + ManagementObject + "></td>";
                }
                else
                {  
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",3)' target=_self><img style='width:100%;height:200px' src=" + ManagementObject + "></td>";
                }
            }
            if (ManagementSlogans != null)
            {
                if (this.drpDepartment.SelectedItem.Text.Contains("本部"))
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",4)' target=_self><img style='width:100%;height:200px' src=" + ManagementSlogans + "></td>";
                }
                else
                {
                    DepartmentSlogans += "<td align=center><a href='javascript:openimg(" + this.drpDepartment.SelectedValue + ",4)' target=_self><img style='width:100%;height:200px' src=" + ManagementSlogans + "></td>";
                }
            }
            DepartmentSlogans += "</tr></table>";
        }
    }


    /// <summary>
    /// 绑定下拉列表
    /// </summary>
    /// <param name="_drp"></param>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpYear))
        {
            this.drpYear.DataSource = new BLL.StrategicDateBll().get();
            this.drpYear.DataTextField = "StrategicDate";
            this.drpYear.DataValueField = "Id";
            this.drpYear.DataBind();
        }
        if (_drp.Equals(this.drpDepartment))
        {
            this.drpDepartment.DataSource = new BLL.DepartmentInfoManager().getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门");
            this.drpDepartment.Items.Insert(0, _li);
            this.drpDepartment.SelectedValue = Utility.HttpHelp.getParamsString("Depart");
        }

    }


    void bindGridView()
    {
        StrategicPlanMgr dllStrategicPlanMgr = new StrategicPlanMgr();
        object[] _value = new object[2];
        _value[0] = Convert.ToInt32(this.drpYear.SelectedValue);
        _value[1] = this.drpDepartment.SelectedItem.Text;
        DataTable dt = dllStrategicPlanMgr.get(StrategicPlanMgr.field.StrategicPlanDate.ToString(), _value);
        this.gdvTotalPlan.DataSource = dt;
        this.gdvTotalPlan.DataBind();
    }


    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
    }


    /// <summary>
    /// 查看分解年计划
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnViewQuarter_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引
        string Id = this.gdvTotalPlan.DataKeys[_rowIndex]["StrategicPlanId"].ToString();
        //得到选择的战略周期值         
        string _url = "/StrategicPlan/ViewQuarterPlan.aspx?Year=" + this.drpYear.SelectedItem.Text + "&StrategicPlanId=" + Id + "&Department=" + this.drpDepartment.SelectedItem.Text;
        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=600,width=1000,top=200,left=200,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        return;
    }
    protected void btnImport_Click(object sender, EventArgs e)
    {
        //得到选择的战略周期值         
        string _url = "StrategicPlanImport.aspx?StrategicDate=" + this.drpYear.SelectedValue;
        Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=160,width=900,top=300,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        return;
    }


    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
    }
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        string department = drpDepartment.SelectedItem.Value;

        string _url = "StrategicPlanListDisplay.aspx?depart=" + department;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','newwindow','top=0,left=0,toolbar=no,menubar=no,fullscreen=1,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int strtegicPlanId = 0;
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvTotalPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                strtegicPlanId = Convert.ToInt32(this.gdvTotalPlan.DataKeys[i]["StrategicPlanId"].ToString());
                object[] _value = new object[1];
                _value[0] = strtegicPlanId;
                int val = new BLL.StrategicPlanMgr().del(BLL.StrategicPlanMgr.field.StrategicPlanId.ToString(), _value);
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
        }
        bindGridView();
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int strtegicPlanId = 0;
        for (int i = 0; i < this.gdvTotalPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvTotalPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                strtegicPlanId = Convert.ToInt32(this.gdvTotalPlan.DataKeys[i]["StrategicPlanId"].ToString());
            }
        }
        if (_count == 1)
        {
            string _url = "ModifyStrategicPlan.aspx?StrategicPlanId=" + strtegicPlanId + "&Depart=" + this.drpDepartment.SelectedValue;
            Page.RegisterStartupScript("windowsopen1", "<script language=javascript>window.open('" + _url + "','newwindow','height=300px,width=1000px,top=100,left=100,toolbar=no,menubar=no,fullscreen=0,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        }
        else
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
        }
    }
}
